//
//  ProdSkuView.m
//  JiaoX
//
//  Created by yinqy on 15/1/14.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ProdSkuView.h"
#import "SkuSelectView.h"

#define PROD_IMG_HEIGHT [DimensUtils getScaleX:76]
#define PROD_NAME_FONT_SIZE [DimensUtils getScaleX:16]

#define TITLE_FONT_SIZE [DimensUtils getScaleX:18]

@interface ProdSkuView()
{
    UIScrollView *_scrollView;
    UIView *_bottomView;
    UIImageView *_prodImage;
    UILabel *_nameLabel;
    UILabel *_priceLabel;
    
//    UIView *_leftView;
    
    UILabel *_standardLabel;//规格label
    
    UILabel *_numLabel;
    
    SkuSelectView *_skuSelectView;
    
    UIButton *_buyBtn;
}

@end
@implementation ProdSkuView

-(instancetype)initWithProdDetail:(ProductDetail *)prodDetail{
    
    self = [super init];
    if (self) {
        self.frame = CGRectMake(JXScreenW, 0, JXScreenW-SKU_MARGINLEFT, JXScreenH);
        [self setBackgroundColor:[UIColor clearColor]];
        
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height-49)];
        [_scrollView setBackgroundColor:[UIColor whiteColor]];
        [self addSubview:_scrollView];
        
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height-49, JXScreenW-SKU_MARGINLEFT, 49)];
        [_bottomView setBackgroundColor:JXColor(238, 238, 238)];
        UIView *view = [[UIView alloc]init];
        [view setFrame:CGRectMake(0, 0, _bottomView.width, 0.5)];
        [view setBackgroundColor:JXColor(204, 204, 204)];
        [_bottomView addSubview:view];
        
        _buyBtn = [[UIButton alloc]initWithFrame:CGRectMake(JXPublicInset, (49-35)/2, _scrollView.width-JXPublicInset*2, 35)];
        [_buyBtn setBackgroundImage:[UIImage resizedImage:@"btn_login_bg"] forState:UIControlStateNormal];
        [_buyBtn setBackgroundImage:[UIImage resizedImage:@"btn_disable_bg"] forState:UIControlStateDisabled];
        [_buyBtn.titleLabel setFont:[UIFont systemFontOfSize:[DimensUtils getScaleX:16]]];
        [_buyBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_buyBtn setTitle:@"已抢光" forState:UIControlStateDisabled];
        [_buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:_buyBtn];
        
        [self addSubview:_bottomView];
        
        _prodImage = [[UIImageView alloc]initWithFrame:CGRectMake(JXPublicInset, JXPublicInset, PROD_IMG_HEIGHT, PROD_IMG_HEIGHT)];
        [_prodImage sd_setImageWithURL:[NSURL URLWithString:prodDetail.prodImages[0]]];
        [_scrollView addSubview:_prodImage];
        
        _nameLabel = [[UILabel alloc]init];
        CGFloat nameLabelW = _scrollView.width-JXPublicInset*3-PROD_IMG_HEIGHT;
        [_nameLabel setNumberOfLines:2];
        [_nameLabel setTextColor:[UIColor blackColor]];
        [_nameLabel setFont:[UIFont systemFontOfSize:PROD_NAME_FONT_SIZE]];
        [_nameLabel setFrame:CGRectMake(CGRectGetMaxX(_prodImage.frame)+JXPublicInset, JXPublicInset, nameLabelW, PROD_NAME_FONT_SIZE*2+JXPublicInset)];
        [_nameLabel setText:prodDetail.prodName];
        [_scrollView addSubview:_nameLabel];
        
        _priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(_nameLabel.x, CGRectGetMaxY(_prodImage.frame)-PROD_NAME_FONT_SIZE-5,_nameLabel.width, PROD_NAME_FONT_SIZE)];
        [_priceLabel setTextColor:JXThemeColor];
        [_priceLabel setFont:[UIFont systemFontOfSize:PROD_NAME_FONT_SIZE]];
        [_priceLabel setText:[NSString stringWithFormat:@"￥%.0f",prodDetail.prodNewPrice]];
        [_scrollView addSubview:_priceLabel];
        
        UIView *splite = [[UIView alloc]init];
        [splite setFrame:CGRectMake(0, CGRectGetMaxY(_prodImage.frame)+JXPublicInset, self.width, 0.5)];
        [splite setBackgroundColor:JXColor(204, 204, 204)];
        [_scrollView addSubview:splite];
        
        _standardLabel = [[UILabel alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(splite.frame)+30, self.width-JXPublicInset*2, TITLE_FONT_SIZE)];
        [_standardLabel setFont:[UIFont systemFontOfSize:TITLE_FONT_SIZE]];
        [_standardLabel setText:@"规格"];
        [_scrollView addSubview:_standardLabel];
        
        
        _skuSelectView = [[SkuSelectView alloc]initWithProdSubArray:prodDetail.prodSub];
        _skuSelectView.x = 0;
        _skuSelectView.y = CGRectGetMaxY(_standardLabel.frame)+JXPublicInset;
        [_scrollView addSubview:_skuSelectView];
        
        _numLabel = [[UILabel alloc]initWithFrame:CGRectMake(JXPublicInset, CGRectGetMaxY(_skuSelectView.frame)+30, self.width-JXPublicInset*2, TITLE_FONT_SIZE)];
        [_numLabel setFont:[UIFont systemFontOfSize:TITLE_FONT_SIZE]];
        [_numLabel setText:@"数量"];
        [_scrollView addSubview:_numLabel];
        
        

        _calcView = [[CalcView alloc]initWithWidth:129 Height:34 addImage:@"ic_calc_add" minusImage:@"ic_calc_minus"];
        _calcView.x = JXPublicInset;
        _calcView.y = CGRectGetMaxY(_numLabel.frame)+JXPublicInset;
        [_scrollView addSubview:_calcView];
        
        [_scrollView setBackgroundColor:JXColor(238, 238, 238)];
        
        [_scrollView setContentSize:CGSizeMake(JXScreenW-[DimensUtils getScaleX:50], CGRectGetMaxY(_calcView.frame))];
        if ([prodDetail isCanBuy]==0) {
            [_buyBtn setEnabled:NO];
        }
    }
    return self;
    
}

-(void)click:(UIGestureRecognizer *)ges{
    [UIView animateWithDuration:0.3 animations:^{
        self.x = JXScreenW;
    } completion:^(BOOL finished) {}];
}

-(void)buyBtnClick:(UIButton *)btn{
    if (![self hasOneChecked]) {
        [DialogUtils showTipsWithView:[UIApplication sharedApplication].keyWindow String:@"请先选择一件商品"];
        return;
    }
    if ([self.delegate respondsToSelector:@selector(buyBtnClickWtihCount:)]) {
        [self.delegate buyBtnClickWtihCount:[_calcView getNum]];
    }
}

-(NSUInteger)getSelectIndex{
    return [_skuSelectView getSelectIndex];
}

-(BOOL)hasOneChecked{
    return [_skuSelectView hasOneChecked];
}

-(void)clearSelected{
    [_skuSelectView clearSelected];
}

@end
